package org.example.controller;

import org.example.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Controller
public class UserController {
    // 响应页面/跳转页面
    @RequestMapping("/toJumpPage")
    public String toJumpPage() {
        System.out.println("跳转页面");
        return "page.jsp";
    }

    // 响应文本数据
    @RequestMapping("/toText")
    @ResponseBody
    public String toText() {
        System.out.println("返回纯文本数据");
        return "response text";
    }

    // 响应pojo对象
    @RequestMapping("/toJsonPojo")
    @ResponseBody
    public User toJsonPojo() {
        System.out.println("返回json对象数据");
        User user = new User();
        user.setName("Tom");
        user.setAge(18);
        return user;
    }

    // 响应pojo集合对象
    @RequestMapping("/toJsonList")
    @ResponseBody
    public List<User> toJsonList() {
        System.out.println("返回json集合数据");
        User user1 = new User();
        user1.setName("Tom");
        user1.setAge(18);

        User user2 = new User();
        user2.setName("Tom1");
        user2.setAge(19);

        User user3 = new User();
        user3.setName("Tom2");
        user3.setAge(17);

        List<User> list = new ArrayList<>();
        Collections.addAll(list, user1,user2,user3);
        return list;
    }

}
